package yunjiao.designpatterns.builder;

import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 经典构建
 *
 * @author yangyunjiao
 */
public class UserClassicBuilder {
    private final String firstName;
    private final String lastName;
    private int age;
    private String email;
    private final List<String> phones = new ArrayList<>();

    private UserClassicBuilder(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public static UserClassicBuilder newBuilder(String firstName, String lastName) {
        if (!StringUtils.hasText(firstName)) {
            throw new IllegalArgumentException("FirstName cannot be null or empty");
        }
        if (!StringUtils.hasText(lastName)) {
            throw new IllegalArgumentException("LastName cannot be null or empty");
        }
        return new UserClassicBuilder(firstName, lastName);
    }

    public UserClassicBuilder age(int age) {
        this.age = age;
        return this;
    }

    public UserClassicBuilder email(String email) {
        this.email = email;
        return this;
    }

    public UserClassicBuilder phones(List<String> phones) {
        if (phones == null) {
            throw new IllegalArgumentException("Phones cannot be null");
        }
        this.phones.addAll(phones);
        return this;
    }

    public UserClassicBuilder phone(String phone) {
        if (!StringUtils.hasText(phone)) {
            throw new IllegalArgumentException("Phone cannot be null or empty");
        }
        this.phones.add(phone);
        return this;
    }

    public User build() {
        return new User(firstName, lastName, age, email, phones);
    }
}
